package com.xinqi.modules.user.auth.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AesEncryptUtils {

    /**
     * 加密串
     */
    public static final String SIGNCODE = "xinqiclass230522";

    public static final String IV_STR = "2023060712345678";

    /**
     * 参数分别代表 算法名称/加密模式/数据填充方式
     */
    private static final String ALGORITHMS = "AES/CBC/PKCS5Padding";

    /**
     * 初始化向量(根据需求调整向量的值, 也可以将向量添加到入参变量中)
     */
    private static final byte[] SIV = new byte[16];

    /**
     * 加密
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return 加密后的内容
     * @throws Exception 异常
     */
    public static String encrypt(String content, String encryptKey) throws Exception {
        KeyGenerator.getInstance("AES").init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMS);
        // 加密向量
        IvParameterSpec iv = new IvParameterSpec(SIV);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"), iv);
        byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
        // 采用base64算法进行转码,避免出现中文乱码
        return Base64.encodeBase64String(b);
    }

    /**
     * 加密PC
     *
     * @param content    加密的字符串
     * @param encryptKey key值
     * @return 加密后的内容
     * @throws Exception 异常
     */
    public static String encryptIV(String content, String encryptKey) throws Exception {
        KeyGenerator.getInstance("AES").init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMS);
        // 加密向量
        IvParameterSpec iv = new IvParameterSpec(IV_STR.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"), iv);
        byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
        // 采用base64算法进行转码,避免出现中文乱码
        return Base64.encodeBase64String(b);
    }

    /**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return 解密后的内容
     * @throws Exception 异常
     */
    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        KeyGenerator.getInstance("AES").init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMS);
        // 加密向量
        IvParameterSpec iv = new IvParameterSpec(IV_STR.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"), iv);
        // 采用base64算法进行转码,避免出现中文乱码
        byte[] encryptBytes = Base64.decodeBase64(encryptStr);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    /**
     * 解密PC端传固定偏移向量
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     * @return 解密后的内容
     * @throws Exception 异常
     */
    public static String decryptIV(String encryptStr, String decryptKey) throws Exception {
        KeyGenerator.getInstance("AES").init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMS);
        // 加密向量
        IvParameterSpec iv = new IvParameterSpec(IV_STR.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"), iv);
        // 采用base64算法进行转码,避免出现中文乱码
        byte[] encryptBytes = Base64.decodeBase64(encryptStr);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

}
